<template>
  <div class="person">
    <h1>情况五：监视上述的多个数据</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h3>年龄：{{ person.age }}</h3>
    <h4>汽车：{{ person.car.c1 }}、 {{ person.car.c2 }}</h4>
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changeC1e">修改第一台汽车</button>
    <button @click="changeC2">修改第二台汽车</button>
    <button @click="changeCar">修改整个车</button>
  </div>
  
</template>



<script lang="ts" setup name="Person">
import { reactive, watch } from 'vue'

let person = reactive({
  name: 'John',
  age: 25,
  car: {
    c1: 'Toyota',
    c2: 'Honda'
  }
})

function changeName() {
  person.name += "~"
}
function changeAge() {
  person.age += 1
}
function changeC1e() {
  person.car.c1 += "奥迪"
}
function changeC2() {
  person.car.c2 += "大众"
}
function changeCar() {
  // 整个能改，但里面的属性可以改
  person.car = {
    c1: '雅迪',
    c2: '艾玛'
  }
}

//监视，情况五：监视上述的多个数
//watch([()=>person.name, ()=>person.car.c1], (newVal, oldVal) => {
//  console.log('person.name changed', newVal, oldVal)
//})
watch([()=>person.name, ()=>person.car], (newVal, oldVal) => {
  console.log('person.name changed', newVal, oldVal)
}, {deep: true})



</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px
}

button {
  margin: 0 5px;
}
li {
  font-size: 20px;
}
</style>